package com.prawn.trace.record.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.prawn.trace.record.dao.RecordInvestDao;
import com.prawn.trace.record.pojo.RecordCheck;
import com.prawn.trace.record.pojo.RecordInvest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;
import util.NullAttributeUtils;


/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class RecordInvestService {

	@Autowired
	private RecordInvestDao recordInvestDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<RecordInvest> findAll() {
		return recordInvestDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<RecordInvest> findSearch(Map whereMap, int page, int size) {
		Specification<RecordInvest> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return recordInvestDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<RecordInvest> findSearch(Map whereMap) {
		Specification<RecordInvest> specification = createSpecification(whereMap);
		return recordInvestDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public RecordInvest findById(String id) {
		return recordInvestDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param recordInvest
	 */
	public void add(RecordInvest recordInvest) {
		recordInvest.setId( idWorker.nextId()+"" );
		recordInvestDao.save(recordInvest);
	}

	/**
	 * 修改
	 * @param recordInvest
	 */
	public void update(RecordInvest recordInvest) {
		RecordInvest oldRecordInvest =recordInvestDao.findById(recordInvest.getId()).get();
		BeanUtils.copyProperties(recordInvest,oldRecordInvest, NullAttributeUtils.getNullPropertyNames(recordInvest));
		recordInvestDao.save(oldRecordInvest);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		recordInvestDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<RecordInvest> createSpecification(Map searchMap) {

		return new Specification<RecordInvest>() {

			@Override
			public Predicate toPredicate(Root<RecordInvest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 投入品id
                if (searchMap.get("investId")!=null && !"".equals(searchMap.get("investId"))) {
                	predicateList.add(cb.like(root.get("investId").as(String.class), "%"+(String)searchMap.get("investId")+"%"));
                }
                // 投入品名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 投入的目标产产品批次id
                if (searchMap.get("produceBatchId")!=null && !"".equals(searchMap.get("produceBatchId"))) {
                	predicateList.add(cb.like(root.get("produceBatchId").as(String.class), "%"+(String)searchMap.get("produceBatchId")+"%"));
                }
                // 用法用量
                if (searchMap.get("usage")!=null && !"".equals(searchMap.get("usage"))) {
                	predicateList.add(cb.like(root.get("usage").as(String.class), "%"+(String)searchMap.get("usage")+"%"));
                }
                // 负责人id
                if (searchMap.get("responsiblePerson")!=null && !"".equals(searchMap.get("responsiblePerson"))) {
                	predicateList.add(cb.like(root.get("responsiblePerson").as(String.class), "%"+(String)searchMap.get("responsiblePerson")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.equal(root.get("createBy").as(String.class), (String)searchMap.get("createBy")));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 删除标记（虚拟删除，并不删除数据库表数据，0：正常，1：删除）
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
                // 所属生产流程id
                if (searchMap.get("processId")!=null && !"".equals(searchMap.get("processId"))) {
                	predicateList.add(cb.like(root.get("processId").as(String.class), "%"+(String)searchMap.get("processId")+"%"));
                }
                // 场地编号
                if (searchMap.get("fieldId")!=null && !"".equals(searchMap.get("fieldId"))) {
                	predicateList.add(cb.like(root.get("fieldId").as(String.class), "%"+(String)searchMap.get("fieldId")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
